<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .wooden-button {
            display: inline-block;
            padding: 12px 24px;
            font-size: 16px;
            font-weight: bold;
            text-align: center;
            text-decoration: none;
            cursor: pointer;
            border: none;
            border-radius: 8px;
            background: linear-gradient(to bottom, #b5895a, #936a41);
            color: white;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
            text-shadow: 0 2px 2px rgba(0, 0, 0, 0.3);
            transition: all 0.3s ease;
        }

        .wooden-button:hover {
            background: linear-gradient(to bottom, #936a41, #755434);
            box-shadow: 0 6px 12px rgba(0, 0, 0, 0.4);
        }

        .wooden-button:active {
            transform: translateY(2px);
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
        }

        img {
            width: 150px;
            height: 150px;
        }

        table {
            width: 80%;
            border-collapse: collapse;
            margin-top: 30px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
            animation: fadeIn 0.5s ease-out;
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(20px);
            }

            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        th,
        td {
            padding: 15px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }

        th {
            background-color: #5d6d7e;
            color: white;
            text-transform: uppercase;
            font-weight: 600;
        }

        tr:nth-child(even) {
            background-color: #eaecee;
        }

        tr:nth-child(odd) {
            background-color: #f2f3f4;
        }

        tr:hover {
            background-color: #d0d3d4;
            transform: scale(1.02);
            transition: all 0.3s ease;
        }

        td {
            color: #333;
        }

        td:first-child {
            color: #2874a6;
            font-weight: 500;
        }
    </style>
</head>

<body>
    <h1>电子书管理</h1>
    <div class="tia">
        id：<input type="text">书名：<input type="text">作者：<input type="text">价格：<input type="text">图片：<input type="text">已售：<input type="text">库存：<input type="text">
        <button class="wooden-button" onclick="add()">添加</button>
    </div>

    <table border="1px">
        <thead>
            <tr>
                <th>图书编号</th>
                <th>名称</th>
                <th>作者</th>
                <th>价格</th>
                <th>图片</th>
                <th>已售</th>
                <th>库存</th>
                <th>删除和修改</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td></td>
            </tr>
        </tbody>
    </table>
    <script>
        function apply() {
            var xhr = new XMLHttpRequest();
            xhr.open('get', 'http://127.0.0.1:3000/abbooks');
            xhr.send();
            xhr.onload = () => {
                var data = JSON.parse(xhr.response)
                var arr = data.data.books
                var tbody = document.querySelector('tbody');
                tbody.innerHTML = '';
                arr.forEach(item => {
                    tbody.innerHTML += `
                <tr>
                    <td>${item.id}</td>
                    <td>${item.name}</td>
                    <td>${item.author}</td>
                    <td>${item.price}</td>
                    <td><img src="${item.img}" alt=""></td>
                    <td>${item.sold}</td>
                    <td>${item.stock}</td>
                    <td>
                        <button class="wooden-button">删除</button>
                        <button class="wooden-button">修改</button>
                    </td>
                </tr>
                `
                });
            }
            xhr.onerror = (e) => {
                console.log(e);
            }
        }
        apply();
        function add() {
            var inp1 = document.querySelectorAll('.tia input');[0].value
            var inp2 = document.querySelectorAll('.tia input');[1].value
            var inp3 = document.querySelectorAll('.tia input');[2].value
            var inp4 = document.querySelectorAll('.tia input');[3].value
            var inp5 = document.querySelectorAll('.tia input');[4].value
            var inp6 = document.querySelectorAll('.tia input');[5].value
            var inp7 = document.querySelectorAll('.tia input');[6].value
            var xhr = new XMLHttpRequest();
            xhr.open('post', 'http://127.0.0.1:3000/books');
            xhr.setRequestHeader("Content-Type", "text/html; charset=utf-8");
            var obj = {
                id: inp1,
                name: inp2,
                img: inp3,
                author: inp4,
                price: inp5,
                stock: inp6,
                sold: inp7
            }
            xhr.send();
            xhr.onload = () => {
                apply();
            }
            xhr.onerror = (e) => {
                console.log(e);
            }
        }
    </script>
</body>

</html>